﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software component that provide 
// support for localization. This library enable you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace ReflectionStudio.LocalizationLibrary
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// This class contains all the localizations of the resources used in the project HelloWorld.
    /// </summary>
    public class ResourceManager
    {
        //Constructors
        public string ProcessedTables = "";
        /// <summary>
        /// Default static constructor.
        /// </summary>
        public ResourceManager()
        {
            Resources = new Dictionary<string, string>(); 
        }

        //Readonly properties

        public Dictionary<String, String> Resources{get;set;}

        //Methods

        /// <summary>
        /// Add a resource in the dictionary Resources.
        /// </summary>
        /// <param name="resourceID">Resource identifier.</param>
        /// <param name="langCode">Language ID.</param>
        /// <param name="value">Language ID.</param>
        public void AddResource(String resourceID, String langCode, String value)
        {
            if ((!String.IsNullOrWhiteSpace(resourceID)) &&
                (!String.IsNullOrWhiteSpace(langCode)) &&
                (!String.IsNullOrWhiteSpace(value)))
            {
                String key = String.Format("{0}@{1}", resourceID, langCode);
                if (!Resources.ContainsKey(key))
                {
                    Resources.Add(key, value);
                }
            }
        }
        public void Merge(ResourceManager rm,string table)
        {
            if (ProcessedTables.Contains(table+";"))
                return;
            ProcessedTables += table + ";";
            foreach (KeyValuePair<String, String> kp in rm.Resources)
            {
                 if (!Resources.ContainsKey(kp.Key))
                 {
                     this.Resources.Add(kp.Key,kp.Value);
                 }
            }
        }

        /// <summary>
        /// Gets the localized resource value of the resource that has ResourceID equals to <paramref name="resourceID"/>.
        /// </summary>
        /// <param name="resourceID">Resource identifier.</param>
        /// <param name="langCode">Language code.</param>
        /// <returns>The localized resource value of the resource that has ResourceID equals to <paramref name="resourceID"/>.</returns>
        public String GetResourceValue(String resourceID, String langCode)
        {
            String retVal = null;
            if ((!String.IsNullOrWhiteSpace(resourceID)) &&
                (!String.IsNullOrWhiteSpace(langCode)))
            {
                String key = String.Format("{0}@{1}", resourceID, langCode);
                if (Resources.ContainsKey(key))
                {
                    retVal = Resources[key];
                }
            }
            return retVal;
        }
    }
}